﻿using System.Collections.Generic;

namespace ChengNet.StrongCaml.Node
{
    /// <summary>Do not instantiate directly. Use <see cref="Caml"/>.</summary>
    public class FieldRef : CamlNode
    {
        // http://msdn.microsoft.com/en-us/library/ms442728.aspx#1 - querying multiple items that all match a Lookup Field
        // http://msdn.microsoft.com/en-us/library/ms442728.aspx#2 - querying Lookup Fields with multiple (LogicalJoin'd) Conditions
        // http://msdn.microsoft.com/en-us/library/ms442728%28v=office.12%29.aspx#9 - querying for a User by display name, by login name, and by user ID
        // set by parent Condition
        private bool lookupId = false;
        /// <summary>
        /// Accessed by parent Condition. Will be set to true if this field is associated with a Value of type Lookup or User.
        /// </summary>
        internal bool LookupId {
            set {
                if (value)
                    Attributes["LookupId"] = true.ToString().ToUpper(); // sets or adds attribute
                else
                    Attributes.Remove("LookupId");
                lookupId = value;
            }
        }

        internal FieldRef(string internalName, SortOrder sortType = SortOrder.Ascending) {
            Attributes = new Dictionary<string, string>() { { "Name", Encode.ScriptEncode(internalName) } };
            if (sortType == SortOrder.Descending) Attributes.Add("Ascending", false.ToString().ToUpper());

            // TODO: untested
            //if (lookupValue) Attributes.Add("LookupValue", true.ToString().ToUpper());
        }
    }
}
